package com.zhishu.portal.service;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.dao.impl.NutDao;
import org.springframework.stereotype.Component;

import com.zhishu.model.Result;
import com.zhishu.model.User;
import com.zhishu.portal.util.CheckUtil;
import com.zhishu.portal.util.Const;
import com.zhishu.portal.util.RedisUtil;
import com.zhishu.portal.util.SendMsgUtil;

@Component
public class UserService {

	private static Logger logger = Logger.getLogger(UserService.class);
	
	@Resource
	private NutDao mysqlDao;


	public Result regUser(User user) {

		Result ret = new Result();
		try {
			if (CheckUtil.checkEmail(user.getUser_name())) {
				user.setEmail(user.getEmail());
			}
			if (CheckUtil.checkMobileNumber(user.getUser_name())) {
				user.setMobile(user.getUser_name());
			}
			String rcode = RedisUtil.getString(user.getUser_name());

			if (StringUtils.isEmpty(rcode)) {
				ret.setResult(Const.FAIL, "验证码已经过期");
			} else {
				if (!rcode.equals(user.getCode())) {
					ret.setResult(Const.FAIL, "验证码不一致");
				} else {

					User u = mysqlDao.fetch(User.class, Cnd.where("user_name", "=", user.getUser_name()));
					if (u != null) {

						ret.setResult(Const.FAIL, "手机号或者邮箱已经存在");
						return ret;
					}
					user.setStatus(1);
					user.setCreate_time(new Date());

					mysqlDao.fastInsert(user);

					ret.setResult(Const.SUC, user);
				}
			}

		} catch (Exception e) {
			ret.setResult(Const.FAIL, "注册出错");
			logger.error("检查用户名密码出错", e);

		}
		return ret;
	}

	public Result login(String username, String password, String code) {
		Result ret = new Result();
		try {

			// String rcode = RedisUtil.getString(username);
			//
			// if (StringUtils.isEmpty(rcode)) {
			// ret.setResult(Const.FIAL, "验证码已经过期");
			// } else {
			// if (!rcode.equals(code)) {
			// ret.setResult(Const.FIAL, "验证码不一致");
			// } else {
			User user = mysqlDao.fetch(User.class, Cnd.where("user_name", "=", username).and("password", "=", password));
			if (user != null) {

				ret.setResult(Const.SUC, user);

			} else {
				ret.setResult(Const.FAIL, "用户名或密码错误");
			}
			// }
			//
			// }

		} catch (Exception e) {
			logger.info("", e);
		}
		return ret;
	}

	public Result sendCode(String username, String type) {
		Result ret = new Result();
		String code = "";
		if (type.equals("phone")) {
			try {
				code = SendMsgUtil.SendPhoneCode(username);
				RedisUtil.setexString(username, code, 1800);
				ret.setResult(Const.SUC, "发送成功");
			} catch (Exception e) {
				logger.error("", e);
				ret.setResult(Const.FAIL, "发送短信异常");
			}

		} else {
			try {
				code = SendMsgUtil.SendMailCode(username);
				RedisUtil.setexString(username, code, 1800);
				ret.setResult(Const.SUC, "发送成功");
			} catch (Exception e) {
				ret.setResult(Const.FAIL, "发送邮件异常");
			}
		}

		return ret;
	}

}
